package com.ecommerce.service;

import com.ecommerce.entity.User;
import com.ecommerce.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public Map<String, Object> register(String username, String email, String password) {
        Map<String, Object> response = new HashMap<>();
        
        // 检查用户名是否已存在
        if (userRepository.existsByUsername(username)) {
            response.put("success", false);
            response.put("message", "用户名已存在");
            return response;
        }
        
        // 检查邮箱是否已存在
        if (userRepository.existsByEmail(email)) {
            response.put("success", false);
            response.put("message", "邮箱已存在");
            return response;
        }
        
        // 创建新用户
        User user = new User();
        user.setUsername(username);
        user.setEmail(email);
        user.setPassword(passwordEncoder.encode(password));
        
        User savedUser = userRepository.save(user);
        
        response.put("success", true);
        response.put("message", "注册成功");
        response.put("user", Map.of(
            "id", savedUser.getId(),
            "username", savedUser.getUsername(),
            "email", savedUser.getEmail()
        ));
        
        return response;
    }

    public Map<String, Object> login(String username, String password) {
        Map<String, Object> response = new HashMap<>();
        
        Optional<User> userOpt = userRepository.findByUsername(username);
        if (userOpt.isEmpty()) {
            response.put("success", false);
            response.put("message", "用户名或密码错误");
            return response;
        }
        
        User user = userOpt.get();
        if (!passwordEncoder.matches(password, user.getPassword())) {
            response.put("success", false);
            response.put("message", "用户名或密码错误");
            return response;
        }
        
        if (!user.isEnabled()) {
            response.put("success", false);
            response.put("message", "账户已被禁用");
            return response;
        }
        
        // 生成简单的token（实际项目中应该使用JWT）
        String token = "token_" + user.getId() + "_" + System.currentTimeMillis();
        
        response.put("success", true);
        response.put("message", "登录成功");
        response.put("token", token);
        response.put("user", Map.of(
            "id", user.getId(),
            "username", user.getUsername(),
            "email", user.getEmail()
        ));
        
        return response;
    }

    public Map<String, Object> getUserInfo(Long userId) {
        Map<String, Object> response = new HashMap<>();
        
        Optional<User> userOpt = userRepository.findById(userId);
        if (userOpt.isEmpty()) {
            response.put("success", false);
            response.put("message", "用户不存在");
            return response;
        }
        
        User user = userOpt.get();
        response.put("success", true);
        response.put("user", Map.of(
            "id", user.getId(),
            "username", user.getUsername(),
            "email", user.getEmail(),
            "firstName", user.getFirstName(),
            "lastName", user.getLastName(),
            "phoneNumber", user.getPhoneNumber()
        ));
        
        return response;
    }
} 